package com.jali.crud.controller;

import com.jali.crud.bean.JaTask;
import com.jali.crud.bean.JaTaskItem;
import com.jali.crud.bean.Msg;
import com.jali.crud.service.JaTaskItemService;
import com.jali.crud.service.JaTaskService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jali
 * @create 2019-01-06 18:57
 */

@Controller
public class JaTaskController {

    @Autowired
    JaTaskItemService jaTaskItemService;

    @Autowired
    JaTaskService jaTaskService;

    @RequestMapping("/getTaskItemsWithJson")
    @ResponseBody
    public Msg getTaskItemsWithJson(@RequestParam(value = "pn",defaultValue = "1") Integer pn){
        List<JaTaskItem> list = jaTaskItemService.selectTaskItems();
        PageInfo pageInfo = new PageInfo(list,5);
        return Msg.success().add("pageInfo",pageInfo);
    }

    @RequestMapping("/getTaskItems")
    public String getTaskItemsWithJson(){
        return "jaTaskList";
    }

    @RequestMapping("/showTasks")
    @ResponseBody
    public Msg showTasks(){
        List<JaTask> list = jaTaskService.getJaTaskByUserId("1");
        for (JaTask jaTask : list){
            int totalScore = 0;
            int unFinishScore = 0;
            List<JaTaskItem> items = jaTaskItemService.getTaskItemsByParentId(jaTask.getId());
            for(JaTaskItem jaTaskItem : items){
                totalScore+=jaTaskItem.getScore();
                if("0".equals(jaTaskItem.getStatus())){
                    unFinishScore+=jaTaskItem.getScore();
                }
            }
            jaTask.setTotalScore(totalScore);
            jaTask.setUnFinishScore(unFinishScore);
            jaTask.setJaTaskItems(items);
        }
        return Msg.success().add("tasks",list);
    }

    @RequestMapping(value = "/getTaskItem/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Msg getEmp(@PathVariable Integer id){
        JaTaskItem jaTaskItem = jaTaskItemService.getTaskItemById(id);
        return Msg.success().add("item",jaTaskItem);
    }

    @RequestMapping(value = "/taskItem/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public Msg saveTaskItemUseTime(JaTaskItem jaTaskItem){
        jaTaskItemService.updateUseTime(jaTaskItem);
        return Msg.success();
    }
}
